package org.jsbd.boss.action;

import org.apache.commons.lang.StringUtils;
import org.jsbd.boss.common.ResponseUtils;
import org.jsbd.boss.dao.ITaskVerTimerDAO;
import org.jsbd.boss.domian.channel.ApkSimulator;
import org.jsbd.boss.domian.channel.TaskverTimer;
import org.jsbd.boss.domian.channel.VerInfo;
import org.jsbd.boss.service.IApkSimulatorService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @desc
 * @Date 2018/6/12 16:35
 * @Author xupengcheng [xupengcheng@zhexinit.com]
 */
@RequestMapping("/taskverTimer")
@Controller
public class TaskVerTimerController {

    public static final Logger LOGGER = LoggerFactory.getLogger(TaskVerTimerController.class);

    @Resource
    private ITaskVerTimerDAO taskverTimerDAO;

    @Resource
    private ITaskVerTimerDAO taskVerTimerDAO;



    @RequestMapping(value = "list", method = RequestMethod.GET)
    public String list() {
        return "/taskverTimer/list";
    }

    @RequestMapping(value = "listdata")
    @ResponseBody
    public Map<String, Object> listdata(TaskverTimer timerFilter) {
        try {
            Map<String, Object> map = new HashMap<String, Object>();
            Integer isExec = timerFilter.getIsExec();
            if (isExec == null || isExec == -1) {
                timerFilter.setIsExec(null);
            }
            String sysver = timerFilter.getSysver();
            if(StringUtils.isBlank(sysver)){
                timerFilter.setSysver(null);
            }
            map.put("total", taskverTimerDAO.countList(timerFilter));
            map.put("rows", taskverTimerDAO.selectPageList(timerFilter));
            return map;
        } catch (Exception e) {
            LOGGER.error("查询出错", e);
        }
        return null;
    }

    @RequestMapping(value = "add", method = RequestMethod.GET)
    public String add() {
        return "/taskverTimer/add";
    }


    @RequestMapping(value = "save", method = RequestMethod.POST)
    public void save(TaskverTimer timerFilter, HttpServletResponse response) {
        try {
            taskverTimerDAO.insert(timerFilter);
            ResponseUtils.responseSuccess(response);
        } catch (Exception e) {
            LOGGER.error("保存出错", e);
            ResponseUtils.responseFailure(response);
        }
    }

    @RequestMapping(value = "update", method = RequestMethod.POST)
    public void update(TaskverTimer timerFilter, HttpServletResponse response) {
        try {
            taskverTimerDAO.updateByPrimaryKey(timerFilter);
            ResponseUtils.responseSuccess(response);
        } catch (Exception e) {
            LOGGER.error("更新出错", e);
            ResponseUtils.responseFailure(response);
        }
    }

    @RequestMapping(value = "{id}/update")
    public String update(TaskverTimer taskverTimer, Model model) {
        List<TaskverTimer> timerFilters = taskverTimerDAO.selectList(taskverTimer);
        model.addAttribute("config", timerFilters.get(0));
        model.addAttribute("sysVerList", timerFilters);
        return "/taskverTimer/update";
    }


    @RequestMapping(value = "del")
    public void del(TaskverTimer timerFilter, HttpServletResponse response) {
        try {
            taskverTimerDAO.deleteByPrimaryKey(timerFilter.getId());
            ResponseUtils.responseSuccess(response);
        } catch (Exception e) {
            LOGGER.error("删除出错", e);
            ResponseUtils.responseFailure(response);
        }
    }
}
